// 导航守卫逻辑
import router from './router/index'
import store from './store/index'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 导航守卫
const whiteList = ['/login', '/404']
router.beforeEach(async (to, from, next) => {
  if (store.getters.token) {
    // 登录了
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.userId) {
        // 没有用户资料
        await store.dispatch('user/getUserInfoAction')
      }
      // 登录了，去其他界面
      next()
    }
  } else {
    // 未登录
    if (whiteList.includes(to.path)) {
      // 在白名单中
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()
})
router.afterEach((to, from) => {
  NProgress.done()
})
